package com.onluy.frameworkcloud.system.converter;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.onluy.frameworkcloud.Param.RespEncrypt;
import com.onluy.frameworkcloud.tools.utils.JsonUtil;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.onluy.frameworkcloud.tools.utils.ParamTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.util.StreamUtils; 

public class JsonHttpMessageConverter extends FastJsonHttpMessageConverter
{
  protected Logger log =   LoggerFactory.getLogger(this.getClass());

  private SerializerFeature[] serializerFeatures = { SerializerFeature.WriteDateUseDateFormat };


  //是否加密解密
  private boolean encrypt=true;
  public boolean isEncrypt() {
    return encrypt;
  }

  public void setEncrypt(boolean encrypt) {
    this.encrypt = encrypt;
  }


  //秘钥
  private String key="e036hlqtculakx38tjw2u9gs";
  public String getKey() {
    return key;
  }

  public void setKey(String key) {
    this.key = key;
  }


  @Autowired
  private HttpServletRequest request;

  protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { String text = "{}";
    if ((obj instanceof List)) {
      List dataset = (List)obj;
      String actionType = this.request.getParameter("__actionType");
      log.info(" <<<<<<<<<<<<<<<<<<<< JsonHttpMessageConverter.writeInternal actionType:" +actionType);

      if ("export".equals(actionType)) {
        try {
          HttpServletResponse response = ((ServletServerHttpResponse)outputMessage).getServletResponse();
          String exportName = this.request.getParameter("exportName");
          //ExportUtil.exportExcel(this.request, response, dataset, this.request.getParameter("headerJSON"), StringUtils.isEmpty(exportName) ? "exportfile" : exportName);
        } catch (Exception e) {
          this.log.error(e.getMessage(), e);
        }
        return;
      }

      if ("expand".equals(actionType)) {
        text = JsonUtil.toJson(dataset);
      }
      else
      {
        text = JSON.toJSONString(obj, getFeatures());
      }
    }  else {
      text = JSON.toJSONString(obj, getFeatures());
    }

    if ("true".equals(this.request.getParameter("JQUERY_FORM_AJAX_REQUEST"))) {
      MediaType mediaType = new MediaType(MediaType.TEXT_HTML.getType(), MediaType.TEXT_HTML.getSubtype(), getCharset());
      outputMessage.getHeaders().setContentType(mediaType);
    }


    if(isEncrypt()) {
      log.info(" <<<<<<<<<<<<<<<<<<<< JsonHttpMessageConverter.writeInternal text 加密前: " + text);
      RespEncrypt respEncrypt = new RespEncrypt();
      respEncrypt.setRespInfo(ParamTool.transRespInfo(text,getKey()));
      log.info(" <<<<<<<<<<<<<<<<<<<< JsonHttpMessageConverter.writeInternal text 加密后: " + respEncrypt.getRespInfo());
      text=JSON.toJSONString(respEncrypt, getFeatures());
    }

    log.info(" <<<<<<<<<<<<<<<<<<<< JsonHttpMessageConverter.writeInternal text:" +text);
    StreamUtils.copy(text, getCharset(), outputMessage.getBody());
  }

  public SerializerFeature[] getFeatures()
  {
    return this.serializerFeatures;
  }
}